package cool.mtc.minghe.service.service.sys.permission.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cool.mtc.core.exception.ServiceException;
import cool.mtc.core.page.PageData;
import cool.mtc.minghe.data.entity.sys.permission.PermissionDO;
import cool.mtc.minghe.data.entity.sys.role.RolePermissionDO;
import cool.mtc.minghe.data.model.sys.permission.dto.PermissionDTO;
import cool.mtc.minghe.data.model.sys.permission.dto.PermissionListDTO;
import cool.mtc.minghe.data.model.sys.permission.form.PermissionForm;
import cool.mtc.minghe.data.model.sys.permission.query.PermissionQuery;
import cool.mtc.minghe.service.mapper.sys.permission.PermissionMapper;
import cool.mtc.minghe.service.mapper.sys.role.RolePermissionMapper;
import cool.mtc.minghe.service.service.BaseServiceImpl;
import cool.mtc.minghe.service.service.sys.permission.PermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 明河
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PermissionServiceImpl extends BaseServiceImpl implements PermissionService {
    private final PermissionMapper permissionMapper;
    private final RolePermissionMapper rolePermissionMapper;

    @Override
    @Transactional
    public long insert(PermissionForm form) {
        PermissionDO permissionDO = form.transToDO();
        permissionDO.setCreator(userId());
        permissionMapper.insert(permissionDO);
        return permissionDO.getId();
    }

    @Override
    @Transactional
    public void update(PermissionForm form) {
        PermissionDO permissionDO = permissionMapper.selectById(form.getId());
        if (null == permissionDO) {
            throw new ServiceException("当前数据已过期，请刷新后重新！(i18n)");
        }
        form.updateDO(permissionDO);
        permissionMapper.updateById(permissionDO);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        if (null == id) {
            return;
        }
        PermissionDO permissionDO = permissionMapper.selectById(id);
        if (null == permissionDO) {
            return;
        }
        this.handleCheckCanDelete(permissionDO);
        permissionMapper.deleteById(id);
    }

    /**
     * 检查是否可以删除
     */
    private void handleCheckCanDelete(PermissionDO permissionDO) {
        if (Boolean.TRUE.equals(permissionDO.getPreset())) {
            throw new ServiceException("preset.delete.not_allow");
        }
        QueryWrapper<RolePermissionDO> wrapper = new QueryWrapper<>();
        wrapper.eq("permission_id", permissionDO.getId());
        long usedCount = rolePermissionMapper.selectCount(wrapper);
        if (0 == usedCount) {
            return;
        }
        throw new ServiceException("todo 当前数据已关联其他数据，无法删除！");
    }

    @Override
    public PageData<PermissionListDTO> pageList(PermissionQuery query) {
        OrderItem[] items = {
                OrderItem.asc("P.order_index"),
        };
        Page<PermissionListDTO> page = permissionMapper.pageList(super.pageParam(items), query);
        return PageData.of(page.getRecords(), page.getTotal());
    }

    @Override
    public PermissionDTO info(Long id) {
        if (null == id) {
            return null;
        }
        return permissionMapper.getById(id);
    }
}
